package com.zd.room.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zd.room.domain.Room;
import com.zd.room.service.RoomService;
import com.zd.room.vo.RoomVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 户Controller
 * 
 * @author fxiao
 * @date 2020-08-12
 */
@Controller
@RequestMapping("/place/room")
public class RoomController extends BaseController
{
    private String prefix = "place/room";

    @Autowired
    private RoomService roomService;

    @RequiresPermissions("place:room:view")
    @GetMapping()
    public String room()
    {
        return prefix + "/room";
    }

    /**
     * 查询户列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RoomVo roomVo)
    {
        startPage();
//        List<Room> list = roomService.selectRoomList(room);
        List<RoomVo>roomVoList=roomService.selectRoomVoList(roomVo);
        return getDataTable(roomVoList);
    }

    /**
     * 选择房间号的弹窗
     * @param roomVo
     * @return
     */
    @GetMapping("/selectroom")
    public String selectRoomModal(RoomVo roomVo,ModelMap mmap)
    {
        mmap.put("room",roomVo);
        return prefix+"/selectRoomModal";
    }

    /**
     * 导出户列表
     */
    @RequiresPermissions("place:room:export")
    @Log(title = "户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RoomVo roomVo)
    {
        List<RoomVo> list = roomService.selectRoomVoList(roomVo);
        ExcelUtil<RoomVo> util = new ExcelUtil<RoomVo>(RoomVo.class);
        return util.exportExcel(list, "房间信息");
    }

    /**
     * 新增户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存户
     */
    @RequiresPermissions("place:room:add")
    @Log(title = "户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Room room)
    {
        return toAjax(roomService.insertRoom(room));
    }

    /**
     * 修改户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
//        Room room = roomService.selectRoomById(id);
        RoomVo roomVo = roomService.selectRoomVoById(id);
        mmap.put("room", roomVo);
        return prefix + "/edit";
    }

    /**
     * 修改保存户
     */
    @RequiresPermissions("place:room:edit")
    @Log(title = "户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Room room)
    {
        return toAjax(roomService.updateRoom(room));
    }

    /**
     * 删除户
     */
    @RequiresPermissions("place:room:remove")
    @Log(title = "户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(roomService.deleteRoomByIds(ids));
    }
}
